package com.lbx.controller;

import com.lbx.entity.Role;
import com.lbx.mapper.RoleMapper;
import com.lbx.util.CommonRes;
import com.lbx.util.ResUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * (Role)表控制层
 *
 * @author lbx
 * @since 2021-06-18 15:30:56
 */
@RestController
@CrossOrigin
@RequestMapping("vue-element-admin")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleMapper roleMapper;
    
    @RequestMapping("/roles")
    public CommonRes<Object> roles(){
        return ResUtil.successRes(20000,"success",roleMapper.queryAll(null));
    }


    
    @PutMapping("role/{key}")
    public CommonRes<Object> updateRole(@PathVariable(value = "key") String key,@RequestBody HashMap<String,Object> map){
    
        List<HashMap<String,Object>> routes = (List<HashMap<String, Object>>) map.remove("routes");
        roleMapper.update(map);
        roleMapper.deleteRoleAndRoute((Integer) map.get("id"));
        if (routes.size()>0) {
            ArrayList<Integer> routeId = new ArrayList<>();
            for (HashMap<String, Object> route : routes) {
                Integer id = (Integer) route.get("id");
                List<HashMap<String,Object>> childrenRoutes = (List<HashMap<String, Object>>) route.get("children");
                for (HashMap<String, Object> childrenRoute : childrenRoutes) {
                    Integer childId = (Integer) childrenRoute.get("id");
                    routeId.add(childId);
                }
                routeId.add(id);
            }
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("rid", map.get("id"));
            hashMap.put("tids",routeId);
            roleMapper.insertRoleAndRoute(hashMap);
        }
        System.out.println(key);
        System.out.println(map);
        return ResUtil.successRes(20000,"success",map);
    }
    
    /**
     * @param map 角色信息+角色拥有的 路径信息
     * @return com.lbx.util.CommonRes<java.lang.Object>
     * @apiNote role 添加 角色
     */
    @RequestMapping("/role")
    public CommonRes<Object> role(@RequestBody HashMap<String,Object> map){
        List<HashMap<String,Object>> routes = (List<HashMap<String, Object>>) map.remove("routes");
        map.put("key",map.get("name"));
        roleMapper.insertOne(map);
        if (routes.size()>0) {
            ArrayList<Integer> routeId = new ArrayList<>();
            for (HashMap<String, Object> route : routes) {
                Integer id = (Integer) route.get("id");
                List<HashMap<String,Object>> childrenRoutes = (List<HashMap<String, Object>>) route.get("children");
                for (HashMap<String, Object> childrenRoute : childrenRoutes) {
                    Integer childId = (Integer) childrenRoute.get("id");
                    routeId.add(childId);
                }
                routeId.add(id);
            }
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("rid",(BigInteger) map.get("id"));
            hashMap.put("tids",routeId);
            roleMapper.insertRoleAndRoute(hashMap);
        }
        return ResUtil.successRes(20000,"success",map);
    }
    
    
}
